package com.opal.system.service.impl;

import com.opal.system.entity.table.OpalPermission;
import com.opal.system.entity.table.OpalPermissionExample;
import com.opal.system.entity.table.OpalRole;
import com.opal.system.mapper.mysql.OpalPermissionMapper;
import com.opal.system.mapper.mysql.OpalPermissionMapperExtend;
import com.opal.system.service.OpalPermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class OpalPermissionServiceImpl implements OpalPermissionService{
	
	@Autowired
	private OpalPermissionMapperExtend opalPermissionMapperExtend;
	@Autowired
	private OpalPermissionMapper opalPermissionMapper;

	@Override
	public List<OpalPermission> selectOpalPermission() {
		return opalPermissionMapper.selectByExample(new OpalPermissionExample());
	}

	@Override
	public int deleteOpalPermissionById(Long id) {

		return opalPermissionMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<OpalPermission> selectOpalPermissionByCondition(OpalPermission opalPermission) {
		return opalPermissionMapper.selectByExample(getOpalPermissionExample(opalPermission));
	}

	@Override
	public int updateOpalPermissionByRoleId(Long roleId) {
		//条件为角色id，修改值又是角色id，需要设置为null  使用逆向工程的类太麻烦了
		try{
			OpalPermission opalPermission = new OpalPermission();
			opalPermission.setRoleId(roleId);
			//第一步根据角色id获取OpalPermission集合数据
			List<OpalPermission> opalPermissions = selectOpalPermissionByCondition(opalPermission);
			for(OpalPermission o:opalPermissions){
				//第二步 根据查询到的id来把角色id修改为null
				o.setRoleId(null);//把角色id设置为null
				opalPermissionMapper.updateByPrimaryKey(o); //此方法全部进行修改
			}
		}catch (Exception e){
			e.printStackTrace();
			return -1;
		}

		return 1;
	}

	@Override
	public int updateOpalPermissionById(OpalPermission opalPermission) {
		return opalPermissionMapper.updateByPrimaryKeySelective(opalPermission);
	}

	@Override
	public int updateOpalPermissionByCondition(OpalPermission OpalPermission,OpalPermission opalPermissionCondition) {
		//getOpalPermissionExample条件没有主键id 的

		return opalPermissionMapper.updateByExampleSelective(OpalPermission,getOpalPermissionExample(opalPermissionCondition));
	}

	@Override
	public int deleteOpalPermissionByCondition(OpalPermission opalPermission) {

		return opalPermissionMapper.deleteByExample(getOpalPermissionExample(opalPermission));
	}

	@Override
	public Set<String> getByRoleIdListPermissions(List<OpalRole> opalRoles) {

		if (opalRoles !=null && opalRoles.size()>0){
			Set<String> set = new HashSet<String>();
			List<OpalPermission> results=	opalPermissionMapperExtend.getByRoleIdListPermissionMapper(opalRoles);
			for(OpalPermission r:results) {
				set.add(r.getPermissionCode());
			}
			return set;
		}
		return null;
	}

	private OpalPermissionExample getOpalPermissionExample(OpalPermission opalPermission){
		OpalPermissionExample opalPermissionExample =new OpalPermissionExample();
		OpalPermissionExample.Criteria criteria = opalPermissionExample.createCriteria();

		//权限代码
		String permissionCode = opalPermission.getPermissionCode();
		if (!StringUtils.isBlank(permissionCode)){
			criteria.andPermissionCodeEqualTo(permissionCode);
		}

		//权限地址
		String permissionUrl = opalPermission.getPermissionUrl();
		if (!StringUtils.isBlank(permissionUrl)){
			criteria.andPermissionUrlEqualTo(permissionCode);
		}

		//角色id
		Long roleId = opalPermission.getRoleId();
		if (roleId !=null){
			criteria.andRoleIdEqualTo(roleId);
		}
		//权限名称
		String permissionName = opalPermission.getPermissionName();
		if (!StringUtils.isBlank(permissionName)){
			criteria.andPermissionNameEqualTo(permissionName);
		}
		return opalPermissionExample;
	}

}
